/*******************************************************************************
 * Copyright (c) 2010 L.Carbonnaux.
 *******************************************************************************/

package org.lcx.taskvision.core.attribute;

import java.util.EnumSet;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.lcx.taskvision.core.ITaskVisionAttribute;
import org.lcx.taskvision.core.attribute.TaskVisionAttributeMapper.Flag;
import org.lcx.taskvision.core.model.TaskVisionTaskKey.Key;

/**
 * @author Laurent Carbonnaux
 */
public enum TaskVisionAttribute implements ITaskVisionAttribute {

	TASKID(Key.TASKID, "TaskId:", TaskAttribute.TASK_KEY, TaskAttribute.TYPE_SHORT_TEXT, Flag.READ_ONLY),

	SUMMARY(Key.SUMMARY, "Summary:", TaskAttribute.SUMMARY, TaskAttribute.TYPE_SHORT_RICH_TEXT),
	PRIORITY(Key.PRIORITY, "Priority:", TaskAttribute.PRIORITY, TaskAttribute.TYPE_SINGLE_SELECT),
	COMPLETED(Key.COMPLETED, "Completed:", TaskAttribute.STATUS, TaskAttribute.TYPE_SHORT_TEXT, Flag.ATTRIBUTE),
	DESCRIPTION(Key.DESCRIPTION, "Description:", TaskAttribute.DESCRIPTION, TaskAttribute.TYPE_LONG_RICH_TEXT),

	DEFAULT(Key.DEFAULT, null, null, TaskAttribute.TYPE_SHORT_TEXT, Flag.ATTRIBUTE),
//	OWNER(Key.OWNER, "Owner:", TaskAttribute.USER_ASSIGNED, TaskAttribute.TYPE_PERSON, Flag.PEOPLE),
	;

	private final String svKey;

	private final String prettyName;

	private final String taskKey;

	private String type;

	private EnumSet<Flag> flags;

	public static TaskVisionAttribute getByTaskKey(String taskKey) {
		for (TaskVisionAttribute attribute : values()) {
			if (taskKey.equals(attribute.getTaskKey())) {
				return attribute;
			}
		}
		return null;
	}

	public static TaskVisionAttribute getBySvKey(String svKey) {
		for (TaskVisionAttribute attribute : values()) {
			if (svKey.equals(attribute.getSvKey())) {
				return attribute;
			}
		}
		return null;
	}

	TaskVisionAttribute(Key svKey, String prettyName, String taskKey, String type, Flag firstFlag, Flag... moreFlags) {
		this.svKey = svKey.getKey();
		this.taskKey = taskKey;
		this.prettyName = prettyName;
		this.type = type;
		if (firstFlag == null) {
			this.flags = TaskVisionAttributeMapper.NO_FLAGS;
		} else {
			this.flags = EnumSet.of(firstFlag, moreFlags);
		}
	}

	TaskVisionAttribute(Key tracKey, String prettyName, String taskKey, String type) {
		this(tracKey, prettyName, taskKey, type, null);
	}

	public String getTaskKey() {
		return taskKey;
	}

	public String getSvKey() {
		return svKey;
	}

	public String getKind() {
		if (flags.contains(Flag.ATTRIBUTE)) {
			return TaskAttribute.KIND_DEFAULT;
		} else if (flags.contains(Flag.PEOPLE)) {
			return TaskAttribute.KIND_PEOPLE;
		}
		return null;
	}

	public String getType() {
		return type;
	}

	public void setType(String newType) {
		type= newType;
	}

	public boolean isReadOnly() {
		return flags.contains(Flag.READ_ONLY);
	}

	public boolean isHidden() {
		return flags.contains(Flag.HIDDEN);
	}

	@Override
	public String toString() {
		return prettyName;
	}

	public static boolean isKeyAttribute(String tag){
		ITaskVisionAttribute att = getBySvKey(tag);
		if(TaskAttribute.TASK_KEY.equals(att.getTaskKey())) return true;
		return false;
	}
	
}
